package com.stx.controller;

import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.stx.domain.Absent;
import com.stx.domain.Maintaining;
import com.stx.domain.Student;
import com.stx.domain.Teacher;
import com.stx.service.AdminService;
import com.stx.service.BuildService;
import com.stx.service.CeService;
import com.stx.service.CuService;
import com.stx.service.DormService;
import com.stx.service.MaintainedService;
import com.stx.service.MaintainingService;
import com.stx.service.StudentService;
import com.stx.service.TeacherService;

@Controller
// @RequestMapping("TeacherController")
public class TeacherController {
	@Resource
	private AdminService as;
	@Resource
	private BuildService bs;
	@Resource
	private CeService ces;
	@Resource
	private CuService cus;
	@Resource
	private DormService ds;
	@Resource
	private StudentService ss;
	@Resource
	private TeacherService ts;
	@Resource
	private MaintainingService mis;
	@Resource
	private MaintainedService mes;
	
	private List list=new ArrayList();

	private static final Log logger = LogFactory.getLog(StudentController.class);

//	@RequestMapping(value = "teacherLogin.do", method = RequestMethod.GET)
//	public String teacherLogin() {
//		logger.info("GET teacherLogin called");
//		return "teacher/loginTeacher";
//	}

	@RequestMapping(value = "teacherLogin.do", method = RequestMethod.POST)
	public String teacherLogin(int no, String pwd, HttpSession session) {
		logger.info("post teacherLogin called");
		Teacher teacher=null;
		teacher = ts.teacherLogin(no, pwd);
		if (teacher != null) {
			session.setAttribute("teacher", teacher);
			return "teacher/menuTeacher";
		} else
			return "redirect:index.jsp";
	}

	@RequestMapping(value = "menu.do", method = RequestMethod.GET)
	public String menu() {
		logger.info("GET menu called");
		return "teacher/Menu";
	}

	@RequestMapping(value = "selectStudentByT.do", method = RequestMethod.GET)
	public String selectStudent(Model model, HttpSession session) {
		logger.info("GET selectStudent called");
		List<Student> studentList=new ArrayList<Student>();
		Teacher teacher=(Teacher) session.getAttribute("teacher");
		studentList = ss.selectStudent(teacher.getBuilding());
		model.addAttribute("list", studentList);
		return "teacher/selectStudent";
	}

	@RequestMapping(value = "modifyPasswordByT.do", method = RequestMethod.GET)
	public String modifyPasswordByT() {
		logger.info("GET modifyPasswordByT called");
		return "teacher/modifyPasswordByT";
	}

	@RequestMapping(value = "modifyPasswordByT.do", method = RequestMethod.POST)
	public String modifyPasswordByT(Model model, String originalpassword, String oldpassword, String newpassword,
			int no) {
		logger.info("POST modifyPasswordByT called");

		if (originalpassword.equals(oldpassword)) {
			int i = 0;
			i = ts.modifyPasswordByT(no, newpassword);
			if (i == 1) {
				return "teacher/passwordSuccess";
			} else {
				model.addAttribute("msg", "修改失败！");
				return "teacher/modifyPasswordByT";
			}
		} else {
			model.addAttribute("msg", "旧密码错误");
			return "teacher/modifyPasswordByT";
		}
	}

	@RequestMapping(value = "updateStudent.do", method = RequestMethod.GET)
	public String updateStudent(Model model, int no, String name, String major) {
		logger.info("GET updateStudent called");

		try {
			String name1 = new String(name.getBytes("ISO-8859-1"), "UTF-8");
			String major1 = new String(major.getBytes("ISO-8859-1"), "UTF-8");
			model.addAttribute("name", name1);
			model.addAttribute("major", major1);
			model.addAttribute("no", no);
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		return "teacher/updateStudent";
	}

	@RequestMapping(value = "modifyStudent.do", method = RequestMethod.POST)
	public String modifyStudent(Model model, Student student) {
		logger.info("POST modifyStudent called");

		ss.updateStudent(student);

		return "redirect:selectStudentByT.do";

	}

	@RequestMapping(value = "deleteStudent.do", method = RequestMethod.GET)
	public String deleteStudent(int no) {
		logger.info("GET deleteStudent called");

		ss.studentDelete(no);

		return "redirect:selectStudentByT.do";
	}

	@RequestMapping(value = "changeDormTeacher.do", method = RequestMethod.GET)
	public String changeDormTeacher() {
		logger.info("GET changeDormTeacher called");

		return "teacher/changeDormTeacher";

	}

	@RequestMapping(value = "changeDormTeacher.do", method = RequestMethod.POST)
	public String changeDormTeacher(Model model, Student student) {
		logger.info("POST changeDormTeacher called");

		ss.updateDorm(student);

		return "teacher/changeDormTeacher";

	}

	@RequestMapping(value = "selectAbsentRecord.do", method = RequestMethod.GET)
	public String selectAbsentRecord(Model model) {
		logger.info("GET selectAbsentRecord called");
		list = ces.selectAbsentRecord();
		model.addAttribute("list", list);
		return "teacher/selectAbsentRecord";
	}

	@RequestMapping(value = "selectDormByT.do", method = RequestMethod.GET)
	public String selectDormByT(Model model, HttpSession session) {
		logger.info("GET selectDormByT called");
		
		Teacher teacher=(Teacher) session.getAttribute("teacher");

		list = ds.selectDorm(teacher.getBuilding());
		model.addAttribute("list", list);

		return "teacher/showDorm";

	}

	@RequestMapping(value = "selectLivedByT.do", method = RequestMethod.GET)
	public String SelectDrom(Model model, String name) {
		logger.info("GET selectLivedByT called");

		list = ss.selectLived(name);

		model.addAttribute("list", list);

		return "teacher/showLivedStudent";

	}

	@RequestMapping(value = "addAbsentInfo.do", method = RequestMethod.GET)
	public String addAbsentInfo() {
		logger.info("GET addAbsentInfo called");
		return "teacher/addAbsentInfo";

	}

	@RequestMapping(value = "addAbsentInfo.do", method = RequestMethod.POST)
	public String addAbsentInfo(Model model, Absent absent) {
		logger.info("POST addAbsentInfo called");
		
		Student student=ss.selectStudent(absent.getNo());
		int i=cus.addAbsent(absent,student);

		return "redirect:selectAbsentRecord.do";

	}

	@RequestMapping(value = "selectAbsentUploadingByT.do", method = RequestMethod.GET)
	public String selectAbsentUploadingByT(Model model) {
		logger.info("GET selectAbsentUploadingByT called");
		list = cus.selectAbsentUploading();
		model.addAttribute("list", list);
		return "teacher/selectAbsentUploading";

	}

	@RequestMapping(value = "deleteAbsentUploading.do", method = RequestMethod.GET)
	public String deleteAbsentUploading(Model model, int id) {
		logger.info("GET deleteAbsentUploading called");
		cus.absentDelete(id);
		return "redirect:selectAbsentUploadingByT.do";

	}

	@RequestMapping(value = "backSelectAbsentRecord.do", method = RequestMethod.GET)
	public String backSelectAbsentRecord() {
		logger.info("GET backSelectAbsentRecord called");

		return "redirect:selectAbsentRecord.do";

	}
	
	@RequestMapping(value = "selectMaintainingByT.do", method = RequestMethod.GET)
	public String selectMaintainingByT(Model model) {
		logger.info("GET selectMaintainingByT called");
		
		list=mis.selectMaintainingByT();
		model.addAttribute("list",list);
		
		return "teacher/selectMaintaining";

	}
	@RequestMapping(value = "deleteMaintainingByT.do", method = RequestMethod.GET)
	public String deleteMaintainingByT(int id) {
		logger.info("GET deleteMaintainingByT called");
		Maintaining main=mis.selectMaintaining(id);
		mis.deleteMaintainingByT(id);
		mes.addMaintainedByT(main);
		
		return "redirect:selectMaintainingByT.do";

	}
	@RequestMapping(value = "selectMaintainedByT.do", method = RequestMethod.GET)
	public String selectMaintainedByT(Model model) {
		logger.info("GET selectMaintainedByT called");
		list=mes.selectMaintainedByT();
		model.addAttribute("list2",list);
		
		return "teacher/selectMaintained";

	}
	
}
